#include "selscene.h"
#include "item/imageitem.h"
#include "recource.h"
#include "scene/gamescene.h"
#include "sound/GameSounds.h"
#include <QLocale>

enum SelBtn
{
    START_SELB,
    BACK_SELB,
    LEVEL2_SELB,
    LEVEL3_SELB,
    NULL_SELB,
}selBtn(NULL_SELB);

SelScene::SelScene(QObject *parent) :
    QGraphicsScene(parent)
{
    setSceneRect(0.0, 0.0, 360.0, 640.0);
    initItem();
}

void SelScene::initItem()
{
    switch(Recource::m_iLanguage)
    {
    case QLocale::Chinese:
//    case QLocale::HongKong:
    case 255:
    {
        m_bgItem = new ImageItem(*Recource::m_SelBgCnImage,
                                 QRect(QPoint(0,0),Recource::m_SelBgCnImage->size()));
        m_backItem = new ImageItem(*Recource::m_backbtnImage,
                                    QRect(QPoint(0,0),Recource::m_backbtnImage->size()));
    }
        break;
    default:
    {
        m_bgItem = new ImageItem(*Recource::m_SelBgEnImage,
                                 QRect(QPoint(0,0),Recource::m_SelBgEnImage->size()));
        m_backItem = new ImageItem(*Recource::m_backbtnEnImage,
                                    QRect(QPoint(0,0),Recource::m_backbtnEnImage->size()));
    }
        break;
    }

    m_bgItem->setPos(0.0,0.0);
    m_bgItem->setZValue(0.0);
    addItem(m_bgItem);

    m_backItem->setZValue(1.0);
    m_backItem->setPos(111.0,576.0);
    m_backItem->setVisible(false);
    addItem(m_backItem);

    m_startItem = new ImageItem(QImage(":/image/sel/level1_p.jpg"),
                                QRect(QPoint(0,0),QImage(":/image/sel/level1_p.jpg").size()));
    m_startItem->setZValue(1.0);
    m_startItem->setPos(21.0,53.0);
    m_startItem->setVisible(false);
    addItem(m_startItem);

    m_level2Item = new ImageItem(*Recource::m_SelLevel2BtnImage,
                                 QRect(QPoint(0,0),Recource::m_SelLevel2BtnImage->size()));
    m_level2Item->setZValue(1.0);
    m_level2Item->setPos(21.0,216.0);
    if(Recource::m_ilevelScore_2 == -1)
    {
        m_level2Item->setVisible(false);
    }
    addItem(m_level2Item);

    m_level3Item = new ImageItem(*Recource::m_SelLevel3BtnImage,
                                 QRect(QPoint(0,0),Recource::m_SelLevel3BtnImage->size()));
    m_level3Item->setZValue(1.0);
    m_level3Item->setPos(21.0,382.0);
    if(Recource::m_ilevelScore_3 == -1)
    {
        m_level3Item->setVisible(false);
    }
    addItem(m_level3Item);
}

void SelScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(m_backItem->isUnderMouse())
    {
        m_backItem->setVisible(true);
        selBtn = BACK_SELB;

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }
    }
    else if(m_startItem->isUnderMouse())
    {
        m_startItem->setVisible(true);
        selBtn = START_SELB;

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }
    }
    else if(m_level2Item->isVisible() && m_level2Item->isUnderMouse())
    {
        m_level2Item->setImage(*Recource::m_SelLevel2BtnPImage);
        selBtn = LEVEL2_SELB;

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }
    }
    else if(m_level3Item->isVisible() && m_level3Item->isUnderMouse())
    {
        m_level3Item->setImage(*Recource::m_SelLevel3BtnPImage);
        selBtn = LEVEL3_SELB;

        if(Recource::m_bPlaySound)
        {
            Recource::m_gamesound->play(0);
        }
    }
}

void SelScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    switch(selBtn)
    {
    case BACK_SELB:
    {
        m_backItem->setVisible(false);
        if(m_backItem->isUnderMouse())
        {
            Recource::SetMain();
        }
    }
        break;
    case START_SELB:
    {
        m_startItem->setVisible(false);
        if(m_startItem->isUnderMouse())
        {
            Recource::m_gamescene->m_icurlevel = 1;
            Recource::m_gamescene->initgame();
            Recource::SetGame();
        }
    }
        break;
    case LEVEL2_SELB:
    {
        m_level2Item->setImage(*Recource::m_SelLevel2BtnImage);
        if(m_level2Item->isUnderMouse())
        {
            Recource::m_gamescene->m_icurlevel = 2;
            Recource::m_gamescene->initgame();
            Recource::SetGame();
        }
    }
        break;
    case LEVEL3_SELB:
    {
        m_level3Item->setImage(*Recource::m_SelLevel3BtnImage);
        if(m_level3Item->isUnderMouse())
        {
            Recource::m_gamescene->m_icurlevel = 3;
            Recource::m_gamescene->initgame();
            Recource::SetGame();
        }
    }
        break;
    default:
        break;
    }
}
